package com.woniuxy.creditsystem.systemmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.util.TraceUtil;
import com.woniuxy.creditsystem.systemmag.service.IRoleMenuService;
import com.woniuxy.creditsystem.systemmag.service.IRoleService;
import com.woniuxy.creditsystem.systemmag.service.ISystemLogService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 系统日志的增、删、查、改
 * @author hejie
 */
@RequestMapping("/systemLog")
@RestController
public class SystemLogController {

    @Resource
    private ISystemLogService systemLogService;
    //从application.properties中获取poi的excel文件存放路径
    @Value("${poiFilePath}")
    private String poiFilePath;

    /**
     * 把前台传回来的table中的RoleInfo list集合 导出到一个Excel文件
     * @author hejie
     *  @param tableDataBean= tableDataJson(前端表格数据)+ simpleFileNameUserSet（用户定义的表格名字）
     * @return ResultMsg
     */
   @PostMapping(value="/exportList",produces = {"application/json;charset=utf-8"})
    public ResultMsg tableDataJson(@RequestBody TableDataBean<SystemLogInfo> tableDataBean) throws IOException {
       List<SystemLogInfo> tableDataJson=tableDataBean.getTableDataJson();
       String simpleFileNameUserSet=tableDataBean.getSimpleFileNameUserSet();

        systemLogService.write2excel(tableDataJson,poiFilePath,simpleFileNameUserSet);

        System.out.println("\n"+TraceUtil.positionTrace());
        for (SystemLogInfo systemLogInfo:tableDataJson) {
            System.out.println(systemLogInfo);
        }
        ResultMsg resultMsg = new ResultMsg(200,"in tableDataJson()",null);
        return resultMsg;
    }

    /**
     * 分页查询数据
     * @author hejie
     * @param systemLogInfo 查询数据
     * @return
     */
    @RequestMapping(value="/page",produces = {"application/json;charset=utf-8"})
    public PageInfo<SystemLogInfo> findAllByParams(int pageNum, SystemLogInfo systemLogInfo,String userInfoUserName ){
        System.out.println("\ninfindAllByParams...systemLogInfo="+systemLogInfo+"\tuserInfoUserName="+userInfoUserName+TraceUtil.positionTrace());

        //把前台传过来的的操作用户名字，设置到systemLogInfo的userInfo中
        if (systemLogInfo!=null && userInfoUserName!=null){
            UserInfo userInfo= new UserInfo();
            userInfo.setUserName(userInfoUserName);
            systemLogInfo.setUserInfo(userInfo);
        }

        int pageSize=10;

        String excuteTimeStr=null;
        if (systemLogInfo.getExcuteTime()!=null){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
            excuteTimeStr=sdf.format(systemLogInfo.getExcuteTime());
            System.out.println("\nexcuteTimeStr="+excuteTimeStr);
        }
        PageInfo<SystemLogInfo> pageInfo = systemLogService.findAllByParams(pageNum,pageSize,systemLogInfo,excuteTimeStr);
        System.out.println("\npageInfo.getList()="+pageInfo.getList()+TraceUtil.positionTrace());
        return pageInfo;
    }

    /**
     * 删除一些systemLogInfo
     * @author hejie
     * @param ids
     * @return
     */
   @DeleteMapping(value="/{ids}",produces = {"application/json;charset=utf-8"})
    public ResultMsg delete(@PathVariable("ids") Long[] ids){
       for (Long id:ids) {
           System.out.println("id="+id);
       }

        ResultMsg resultMsg = new ResultMsg(200,"删除成功",null);
        for (Long id:ids) {
            systemLogService.deleteSystemLogInfo(id);
        }
        return resultMsg;
    }
}
